﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YiSha.Entity.Business;
using YiSha.Entity.OrganizationManage;
using YiSha.Model.Param.Business;
using YiSha.Model.Param.OrganizationManage;
using YiSha.Service.OrganizationManage;
using YiSha.Util.Extension;
using YiSha.Util.Model;

namespace YiSha.Business.Business
{
    public class PrescriptionBLL
    {
        private PrescriptionService prescriptionService = new PrescriptionService();
        private DepartmentService departmentService = new DepartmentService();

        #region 获取数据
        public async Task<TData<List<PrescriptionEntity>>> GetList(PrescriptionListParam param)
        {
            TData<List<PrescriptionEntity>> obj = new TData<List<PrescriptionEntity>>();
            obj.Data = await prescriptionService.GetList(param);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<PrescriptionEntity>>> GetPageList(PrescriptionListParam param, Pagination pagination)
        {
            TData<List<PrescriptionEntity>> obj = new TData<List<PrescriptionEntity>>();
            obj.Data = await prescriptionService.GetPageList(param, pagination);

            //List<DepartmentEntity> departmentList = await departmentService.GetList(new DepartmentListParam { Ids = obj.Data.Select(p => p.DepartmentId).ParseToStrings<long>() });
            //foreach (PrescriptionEntity person in obj.Data)
            //{
            //    person.DepartmentName = departmentList.Where(p => p.Id == person.DepartmentId).Select(p => p.DepartmentName).FirstOrDefault();
            //}

            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<PrescriptionEntity>> GetEntity(long id)
        {
            TData<PrescriptionEntity> obj = new TData<PrescriptionEntity>();
            obj.Data = await prescriptionService.GetEntity(id);
            obj.Tag = 1;
            return obj;
        }


        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(PrescriptionEntity entity)
        {
            TData<string> obj = new TData<string>();

            if (entity.TreatmentId==0)
            {
                obj.Message = "请选择治疗方案！";
                return obj;
            }
            if (prescriptionService.ExistPersonName(entity))
            {
                obj.Message = "存在未结束的治疗方案！";
                return obj;
            }
            await prescriptionService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }


        /// <summary>
        /// 完成治疗
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<TData<string>> SaveEnd(PrescriptionEntity entity)
        {
            TData<string> obj = new TData<string>();

            if (entity.IsEnd == 1)
            {
                obj.Message = "已经完成治疗！";
                return obj;
            }
            var model = prescriptionService.GetEntity(entity.Id.Value).Result;

            model.IsEnd = 1;
            model.EndTime = System.DateTime.Now;
            model.EndExplain = entity.EndExplain;
            await prescriptionService.SaveForm(model);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }




        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await prescriptionService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion
    }
}
